package org.wisigoth.chat.swing.chatscreen;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JTextPane;

import org.wisigoth.chat.client.ChatSession;
import org.wisigoth.chat.client.Message;
import org.wisigoth.chat.client.MessageConsumer;
import org.wisigoth.chat.client.MessageFilter;
import org.wisigoth.chat.client.msg.ChatMessage;

/*
 * TestChatFrame.java
 *
 * Created on 30 janv. 2009, 11:31:48
 */

/**
 * 
 * @author tof
 */
public class ChatPanel extends javax.swing.JLabel implements MessageConsumer,
		MessageFilter {

	private final ChatSession session;
	private final ChatFrame parent;

	/*
	 * public ChatPanel(ChatFrame parent) { this.parent = parent;
	 * this.initComponents(); }
	 */

	/**
	 * Creates new form TestChatFrame
	 * 
	 * @param contact
	 * @param string
	 */
	public ChatPanel(ChatFrame parent, ChatSession session) {
		this.parent = parent;
		this.session = session;
		this.session.addMessageConsumer(this);
		this.initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		
		this.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
		java.awt.GridBagConstraints gridBagConstraints;

		this.contactPane = new javax.swing.JPanel();
		this.chatScrollPane = new javax.swing.JScrollPane();
		this.chatTextPane = new javax.swing.JTextPane();
		this.promptScrollPane = new javax.swing.JScrollPane();
		this.promptTextPane = new javax.swing.JTextPane();
		this.contactPresencePane = new javax.swing.JLabel();
		this.contactNicknamePane = new javax.swing.JLabel();
		this.contactAvatarPane = new javax.swing.JLabel();

		/**
		 * This part set the contact panel properties
		 */
		//this.contactPane.setBackground(Color.BLUE);
		this.contactPane.setMaximumSize(new java.awt.Dimension(300, 30));
		this.contactPane.setMinimumSize(new java.awt.Dimension(300, 30));
		this.contactPane.setPreferredSize(new java.awt.Dimension(300, 30));
		this.contactPane.setLayout(new java.awt.GridBagLayout());
		GridBagConstraints contactPresenceConstraints = new GridBagConstraints();
		contactPresenceConstraints.gridx = 0;
		contactPresenceConstraints.gridy = 0;
		contactPresenceConstraints.weightx = 1;

		contactPresenceConstraints.fill = GridBagConstraints.HORIZONTAL;
		contactPresenceConstraints.anchor = java.awt.GridBagConstraints.WEST;
		this.contactPane.add(this.contactPresencePane,
				contactPresenceConstraints);
		GridBagConstraints contactNicknameConstraints = new GridBagConstraints();
		contactNicknameConstraints.gridx = 1;
		contactNicknameConstraints.gridy = 0;
		contactNicknameConstraints.weightx = 1;
		contactNicknameConstraints.fill = GridBagConstraints.HORIZONTAL;
		contactNicknameConstraints.anchor = java.awt.GridBagConstraints.WEST;
		this.contactPane.add(this.contactNicknamePane,
				contactNicknameConstraints);
		GridBagConstraints contactAvatarconstraints = new GridBagConstraints();
		contactAvatarconstraints.gridx = 2;
		contactAvatarconstraints.gridy = 0;
		contactAvatarconstraints.fill = GridBagConstraints.BOTH;
		contactAvatarconstraints.anchor = java.awt.GridBagConstraints.EAST;
		contactAvatarconstraints.weightx = 0.0;
		contactAvatarconstraints.weighty = 0.0;
		this.contactPane.add(this.contactAvatarPane, contactAvatarconstraints);

		if (this.session.getContact() == null) {
			this.contactPresencePane.setIcon(null);
			this.contactNicknamePane.setText("");
			this.contactAvatarPane.setIcon(null);
		} else {
			this.contactPresencePane.setIcon(this.session.getContact()
					.getPresence().getPresenceImage());
			this.contactNicknamePane.setText(this.session.getContact()
					.getUser().getNickName());
			this.contactAvatarPane.setIcon(this.session.getContact()
					.getAvatar().getAvatarImage());
		}
		/**
		 * this set the panel layout
		 */

		this.setLayout(new GridBagLayout());

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 0.0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		this.add(this.contactPane, gridBagConstraints);

		this.chatScrollPane.setMinimumSize(new java.awt.Dimension(300, 300));
		this.chatScrollPane.setPreferredSize(new java.awt.Dimension(300, 300));
		this.chatTextPane.setEditable(false);
		this.chatScrollPane.setViewportView(this.chatTextPane);
		this.chatScrollPane
				.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.chatScrollPane.setAutoscrolls(true);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		this.add(this.chatScrollPane, gridBagConstraints);

		this.promptScrollPane.setMinimumSize(new java.awt.Dimension(300, 100));
		this.promptScrollPane
				.setPreferredSize(new java.awt.Dimension(300, 100));
		this.promptScrollPane.setMaximumSize(new java.awt.Dimension(300, 100));
		this.promptScrollPane.setViewportView(this.promptTextPane);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		this.add(this.promptScrollPane, gridBagConstraints);

	}// </editor-fold>

	/**
	 * @param args
	 *            the command line arguments
	 */
	// public static void main(String args[]) {
	// java.awt.EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// new ChatFrame().setVisible(true);
	// }
	// });
	// }
	// Variables declaration - do not modify
	private javax.swing.JScrollPane promptScrollPane;
	private javax.swing.JTextPane promptTextPane;
	private javax.swing.JScrollPane chatScrollPane;
	private javax.swing.JTextPane chatTextPane;
	private javax.swing.JPanel contactPane;
	private javax.swing.JLabel contactPresencePane;
	private javax.swing.JLabel contactNicknamePane;
	private javax.swing.JLabel contactAvatarPane;

	// End of variables declaration

	public javax.swing.JTextPane getPrompt() {
		return this.promptTextPane;

	}

	public javax.swing.JTextPane getChat() {
		return this.chatTextPane;
	}

	public ChatSession getSession() {
		return this.session;
	}

	@Override
	public MessageFilter getFilter() {
		return this;
	}

	@Override
	public void consume(Message message) {
		JTextPane chatArea = this.getChat();
		String chatDisplay = chatArea.getText()
				+ message.getFrom().getNickName() + " : "
				+ ((ChatMessage) message).getText()
				+ System.getProperty("line.separator");
		chatArea.setText(chatDisplay);
	}

	@Override
	public boolean accept(Message message) {
		return true;
	}

	/*
	 * public static void main(String args[]) {
	 * java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new
	 * ChatPanel().setVisible(true); } });
	 * 
	 * 
	 * }
	 */

}
